package com.caifu.bean;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.*;

/**
 * 打印工具类
 * Created by xxj on 07/31.
 */
public final class PrintSocketUtil {
    public static String PRINT_IP = "192.168.1.249";
    public static int PRINT_PORT = 9100;
    public Socket socket = null;
    public OutputStream socketOut = null;
    public OutputStreamWriter writer = null;
    /**
     * @throws IOException
     *
     */
    public PrintSocketUtil() throws IOException {
        // TODO Auto-generated constructor stub
        // 建立打印机连接
        //
        if(socket == null) {
            socket = new Socket(PRINT_IP, PRINT_PORT);
        }else{
            socket.close();
            if(socketOut != null) {
                socketOut.close();
            }
            socket = new Socket(PRINT_IP, PRINT_PORT);
        }
        socketOut = socket.getOutputStream();
        writer = new OutputStreamWriter(socketOut, "GBK");
    }

 /*   public static void main(String[] args) throws IOException {
        //小票页面 公司信息
        Map<String,String> GS_INFO=new HashMap<String,String>();
        GS_INFO.put("GS_Name", "XXXX科技有限公司");
        GS_INFO.put("GS_Address", "武汉市解放大道2679号");
        GS_INFO.put("GS_Tel", "13507115045");
        GS_INFO.put("GS_Qq", "138027869");

        //菜品信息
        List<Map<String, Object>> PRINT_DATA = new ArrayList<Map<String, Object>>();
        Map<String,Object> data1 = new HashMap<String, Object>();
        data1.put("cai_name","小鸡炖蘑菇");
        data1.put("cai_price","36");
        PRINT_DATA.add(data1);


        PrintUtil printLine = new PrintUtil();

        printLine.print(PRINT_DATA,GS_INFO,"123456789");
    }
*/
    /**
     * @param data 数据
     */
    public void print(List<Map<String, Object>> data) {

        try {
            // 条码打印指令
            byte[] PRINT_CODE = new byte[9];
            PRINT_CODE[0] = 0x1d;
            PRINT_CODE[1] = 0x68;
            PRINT_CODE[2] = 120;
            PRINT_CODE[3] = 0x1d;
            PRINT_CODE[4] = 0x48;
            PRINT_CODE[5] = 0x10;
            PRINT_CODE[6] = 0x1d;
            PRINT_CODE[7] = 0x6B;
            PRINT_CODE[8] = 0x02;
            // 清除字体放大指令
            byte[] FD_FONT = new byte[3];
            FD_FONT[0] = 0x1c;
            FD_FONT[1] = 0x21;
            FD_FONT[2] = 4;
            // 字体加粗指令
            byte[] FONT_B = new byte[3];
            FONT_B[0] = 27;
            FONT_B[1] = 33;
            FONT_B[2] = 8;
            // 字体纵向放大一倍
            byte[] CLEAR_FONT = new byte[3];
            CLEAR_FONT[0] = 0x1c;
            CLEAR_FONT[1] = 0x21;
            CLEAR_FONT[2] = 0;
            if(data != null && data.size() > 0) {
                for(Map<String,Object> map : data) {
                    JSONObject info = JSONObject.fromObject(map.get("info"));
                    JSONObject fixedPoint = JSONObject.fromObject(map.get("fixedPoint"));
                    JSONArray toDoor = JSONArray.fromObject(map.get("toDoor"));
                    socketOut.write(27);
                    socketOut.write(64);
                    socketOut.write(FD_FONT);// 字体放大
                    socketOut.write(FONT_B);// 字体加粗
                    socketOut.write(new byte[]{27, 51, (byte) 100});
                    socketOut.write(10);
                    writer.write("          "+ getNotStr(info.get("title")) + " \r\n");
                    writer.write("                    "+getNotStr(info.get("date")) + " \r\n");
                    writer.flush();// 关键,很重要,不然指令一次性输出,后面指令覆盖前面指令,导致取消放大指令无效
                    socketOut.write(CLEAR_FONT);
                    //socketOut.write(10);
                    writer.write("--------------------堂吃/自提-------------------\r\n");
                    writer.write("堂吃/自提      x"+getNotStr(fixedPoint.get("count"))+"\r\n");
                    writer.write(getNotStr(fixedPoint.get("address"))+"\r\n");
                    if(toDoor != null && toDoor.size() > 0) {
                        writer.write("--------------------送餐上门--------------------\r\n");
                        writer.write("送餐上门       x" + toDoor.size() + "\r\n");
                        for(int i = 0 ; i < toDoor.size() ; i ++) {
                            JSONObject obj = toDoor.getJSONObject(i);
                            writer.write(getNotStr(obj.get("user_name")) + "          "+ getNotStr(obj.get("mobile")) +"\n");
                            writer.write(getNotStr(obj.get("address")) + "\r\n");
                        }
                    }
                    writer.flush();

                    //自动切纸
                    CutPage();
                }
            }

            writer.close();
            socketOut.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    /**
     * 字符串长度补齐,方便打印时对齐,美化打印页面,不过中文计算好像有点问题
     *
     * @param strs
     *            源字符
     * @param len
     *            指定字符长度
     * @return
     * @throws UnsupportedEncodingException
     */
    public String Fix_String_Lenth(int type ,String strs, int len) {
        String strtemp = strs;
        int len1 = strs.length();
        switch (type) {
            case 0:
                while (strtemp.length() < len) {
                    strtemp += " ";}
                break;
            case 1:
                while (strtemp.length() < len) {
                    strtemp += " ";}
                break;
            default:

                break;
        }
        return strtemp;
    }

  /*  public PrintUtil CutPage() throws IOException {
        socketOut.write(new byte[]{27, 100, (byte) 5});

        socketOut.flush();
        return this;
    }*/

    public void CutPage() throws IOException {
        socketOut.write(new byte[]{27, 100, (byte) 5});
        socketOut.write(new byte[]{27, 109});
        socketOut.flush();
    }

    public String getNotStr(Object obj){
        if(obj != null && StringUtils.isNotBlank(String.valueOf(obj)) && !"null".equals(String.valueOf(obj))){
            return String.valueOf(obj);
        }else{
            return "";
        }
    }
}
